/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-25 21:12:01  
 * Copyright (c) 2015- -版权所有     
 */

package com.joysuch.wwyt.bp.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.bp.bean.BpAccidentCorrectiveMeasureDetailBean;
import com.joysuch.wwyt.bp.bean.BpAccidentCorrectiveMeasureEditBean;
import com.joysuch.wwyt.bp.bean.BpAccidentCorrectiveMeasureListBean;
import com.joysuch.wwyt.bp.bean.BpAccidentCorrectiveMeasureListBean.SearchBpAccidentCorrectiveMeasure;
import com.joysuch.wwyt.bp.entity.BpAccidentCorrectiveMeasure;
import com.joysuch.wwyt.bp.entity.dsl.QBpAccidentCorrectiveMeasure;
import com.joysuch.wwyt.bp.repository.BpAccidentCorrectiveMeasureDao;
import com.joysuch.wwyt.bp.repository.BpAccidentDao;
import com.joysuch.wwyt.bp.service.BpAccidentCorrectiveMeasureService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.common.util.PageDataUtils.PageDataConvert;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class BpAccidentCorrectiveMeasureServiceImpl implements BpAccidentCorrectiveMeasureService {

    @Autowired
    private BpAccidentCorrectiveMeasureDao dao;
    @Autowired
    private BpAccidentDao bpAccidentDao;
    @Autowired
    private BaseUserDao baseUserDao;

    @Override
    public BpAccidentCorrectiveMeasure add(BpAccidentCorrectiveMeasureEditBean entity) {
        BpAccidentCorrectiveMeasure content = new BpAccidentCorrectiveMeasure();
        BeanUtils.copyProperties(entity, content, "id");
        dao.save(content);
        return content;
    }

    @Override
    public void update(BpAccidentCorrectiveMeasureEditBean entity) {
        BpAccidentCorrectiveMeasure content = dao.findById(entity.getId()).get();
        BeanUtils.copyProperties(entity, content, "id");
        dao.save(content);
    }

    @Override
    public Page<BpAccidentCorrectiveMeasureListBean> findByPage(Pageable page,
            SearchBpAccidentCorrectiveMeasure condition) {
        Predicate predicate = prepareListSearchCondition(condition);

        Page<BpAccidentCorrectiveMeasure> data = predicate == null ? dao.findAll(page) : dao.findAll(predicate, page);

        return PageDataUtils.convertPageData(data,
                new PageDataConvert<BpAccidentCorrectiveMeasureListBean, BpAccidentCorrectiveMeasure>() {

                    @Override
                    public BpAccidentCorrectiveMeasureListBean convert(BpAccidentCorrectiveMeasure f) {
                        BpAccidentCorrectiveMeasureListBean bean = new BpAccidentCorrectiveMeasureListBean();
                        BeanUtils.copyProperties(f, bean);
                        bean.setAccidentName(bpAccidentDao.getNameById(f.getAccidentId()));
                        bean.setInChargeUserName(baseUserDao.getRealNameById(f.getInChargeUserId()));
                        return bean;
                    }

                });
    }

    private Predicate prepareListSearchCondition(SearchBpAccidentCorrectiveMeasure condition) {
        Predicate p = null;
        QBpAccidentCorrectiveMeasure q = QBpAccidentCorrectiveMeasure.bpAccidentCorrectiveMeasure;
        if (condition.getAccidentId() != null) {
            p = ExpressionUtils.and(p, q.accidentId.eq(condition.getAccidentId()));
        }
        if (condition.getNewDiscovery() != null) {
            p = ExpressionUtils.and(p, q.newDiscovery.eq(condition.getNewDiscovery()));
        }
        if (condition.getCorrectiveMeasure() != null) {
            p = ExpressionUtils.and(p, q.correctiveMeasure.eq(condition.getCorrectiveMeasure()));
        }
        if (condition.getInChargeUserId() != null) {
            p = ExpressionUtils.and(p, q.inChargeUserId.eq(condition.getInChargeUserId()));
        }
        if (condition.getRecommandCompleteTime() != null) {
            p = ExpressionUtils.and(p, q.recommandCompleteTime.eq(condition.getRecommandCompleteTime()));
        }
        if (condition.getIndexNo() != null) {
            p = ExpressionUtils.and(p, q.indexNo.eq(condition.getIndexNo()));
        }
        return p;
    }

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     * 
     * @param id
     *            要删除的业务ID
     * @throws BussinessRestrictionsException
     *             抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {
    }

    @Override
    public BpAccidentCorrectiveMeasure findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public BpAccidentCorrectiveMeasureDetailBean getById(Long id) {
        BpAccidentCorrectiveMeasure entity = findById(id);
        if (entity == null) {
            return null;
        }
        BpAccidentCorrectiveMeasureDetailBean bean = new BpAccidentCorrectiveMeasureDetailBean();
        BeanUtils.copyProperties(entity, bean);
        bean.setAccidentName(bpAccidentDao.getNameById(entity.getAccidentId()));
        bean.setInChargeUserName(baseUserDao.getRealNameById(entity.getInChargeUserId()));
        return bean;
    }

    @Override
    public List<BpAccidentCorrectiveMeasureDetailBean> queryDetailList(Long id) {
        List<BpAccidentCorrectiveMeasure> list = dao.findByAccidentId(id);
        return list.stream().map(e -> {
            BpAccidentCorrectiveMeasureDetailBean bean = new BpAccidentCorrectiveMeasureDetailBean();
            BeanUtils.copyProperties(e, bean);
            bean.setInChargeUserName(baseUserDao.getRealNameById(e.getInChargeUserId()));
            return bean;
        }).collect(Collectors.toList());

    }

    @Override
    public void deleteByAccidentId(Long id) {
        dao.deleteByAccidentId(id);
    }

    @Override
    public void deleteByAccidentIdExcludeIds(Long id, List<Long> existingIds) {
        if (CollectionUtils.isEmpty(existingIds)) {
            dao.deleteByAccidentId(id);
        } else {
            dao.deleteByAccidentIdExcludeIds(id, existingIds);
        }
    }

    @Override
    public void saveAll(List<BpAccidentCorrectiveMeasure> measures) {
        dao.saveAll(measures);
    }

}
